package iut2.sponsor.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Gestion des progress bars de l'application
 * 
 */
class ProgressBar extends VerticalPanel {
	protected static final ViewLang lang    = (ViewLang) GWT.create(ViewLang.class);

	/**
	 * Option pour montrer la part restante
	 */
	public static final int SHOW_REMAINING = 1;

	/**
	 * le nombre d'elements a montrer
	 */
	private int width = 20;

	/**
	 * Element text
	 */
	private String type = "";

	/**
	 * Progres courant
	 */
	private int progress = 0;
	
	/**
	 * Nombre maximum
	 */
	private int max = 0;

	/**
	 * Table dans laquelle la progressbar est contenue
	 */
	private FlexTable barFrame = new FlexTable();

	/**
	 * Grille utilisee pour montrer les elements
	 */
	private Grid elementGrid;

	/**
	 * Label en dessous de la barre
	 */
	private Label remainLabel = new Label();


	/**
	 * Flag interne pour les options
	 */
	private boolean showRemaining = false;

	/**
	 * Constructeur de la progressbar
	 * 
	 * @param elements le nombre d'elements a montrer dans la barre
	 * @param options les options d'affichage
	 * @param max le nombre maximum d'elements
	 *            
	 */
	public ProgressBar(String type, int width, int max, int options) {
		//init element text
		this.type = type;
		
		// Read the options and set convenience variables
		if ((options & SHOW_REMAINING) == SHOW_REMAINING)
			showRemaining = true;

		// Set element count
		this.width = width;
		this.max   = max;

		// Styling
		remainLabel.setStyleName("progressbar-remaining");

		// Initialize the progress elements
		elementGrid = new Grid(1, width);
		elementGrid.setCellPadding(0);
		elementGrid.setCellSpacing(0);

		for (int loop = 0; loop < width; loop++) {
			Grid elm = new Grid(1, 1);

			elm.setHTML(0, 0, "");
			elm.setStyleName("progressbar-blankbar");
			elm.addStyleName("progressbar-bar");
			elementGrid.setWidget(0, loop, elm);
		}

		// Create the container around the elements
		Grid containerGrid = new Grid(1, 1);
		containerGrid.setCellPadding(0);
		containerGrid.setCellSpacing(0);
		containerGrid.setWidget(0, 0, elementGrid);

		// Set up the surrounding flex table based on the options
		int row = 0;
		barFrame.setWidget(row++, 0, containerGrid);
		if (showRemaining)
			barFrame.setWidget(row++, 0, remainLabel);

		barFrame.setWidth("100%");

		barFrame.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				popupLegende();
			}
		});
		
		// Add the frame to the panel
		this.add(barFrame);

		// Initialize progress bar
		setProgress(0);
	}

	/**
	 * Affiche l'etat d'avancement courant en temps que pourcentage
	 * 
	 */
	public void setProgress(int nb) {
		float pc = (float)(((float)nb / (float)max) * 100.0);
		int percentage = (int)pc;
			
		// Make sure we are error-tolerant
		if (percentage > 150) percentage = 150;
		if (percentage < 0)   percentage = 0;

		// Set the internal variable
		progress = (int)percentage;

		// Update the elements in the progress grid to
		// reflect the status
		float completed = width * percentage / 150;
		for (int loop = 0; loop < width; loop++) {
			Grid elm = (Grid) elementGrid.getWidget(0, loop);
			if (loop < completed && loop <= (width * 0.66)) {
				elm.setStyleName("progressbar-fullbar");
				elm.addStyleName("progressbar-bar");
			} else if (loop < completed && loop > (width * 0.66)) {
				elm.setStyleName("progressbar-supflbar");
				elm.addStyleName("progressbar-bar");
			} else if (loop <= (width * 0.66)) {
				elm.setStyleName("progressbar-blankbar");
				elm.addStyleName("progressbar-bar");
			} else {
				elm.setStyleName("progressbar-supblbar");
				elm.addStyleName("progressbar-bar");
			}
		}

		if (percentage > 0 && percentage <= 100) {
			// Set the label
			remainLabel.setText(lang.recoltePb(nb, type, percentage));
		} else if (percentage > 0 && percentage > 100) {
			// Set the label
			remainLabel.setText(lang.recolteSupPb((nb - max), type));
		}
	}

	/**
	 * Get the current progress as a percentage
	 * 
	 * @return Current percentage for the progress bar
	 */
	public int getProgress() {
		return (progress);
	}
	
	/**
	 * Montre la legende dans un popup affiche lors d'un clic sur la progressbar
	 */
	public void popupLegende() {
		// Show the popup of contact info
		PopupPanel legendePopup = new PopupPanel(true, false);
	    int left = barFrame.getAbsoluteLeft() + barFrame.getOffsetWidth() - 40;
	    int top = barFrame.getAbsoluteTop() + barFrame.getOffsetHeight() - 15;
	    Grid contenu = new Grid(4,2);
	    contenu.setHTML(0,0,"<div id=\"legende1\" class=\"progressbar-fullbar\"></div>");
	    contenu.setHTML(0,1,lang.pbRecolte());
	    contenu.setHTML(1,0,"<div id=\"legende2\" class=\"progressbar-blankbar\"></div>");
	    contenu.setHTML(1,1,lang.pbNonRecolte());
	    contenu.setHTML(2,0,"<div id=\"legende3\" class=\"progressbar-supflbar\"></div>");
	    contenu.setHTML(2,1,lang.pbDepasse());
	    contenu.setHTML(3,0,"<div id=\"legende3\" class=\"progressbar-supblbar\"></div>");
	    contenu.setHTML(3,1,lang.pbNonDepasse());
	    legendePopup.add(contenu);
	    legendePopup.setPopupPosition(left, top);
	    legendePopup.show();
	}
    
}
